﻿using HCQ2_IBLL;
using HCQ2_Model.VLine.InputDto;
using HCQ2_Model.VLine.OutputDto;
using HCQ2_Model.VLine.Params;
using HCQ2_Model.VLine.Result;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HCQ2_Common;

namespace HCQ2_BLL
{
    public partial class T_VLinesExperienceDetailBLL: IT_VLinesExperienceDetailBLL
    {
        //*********************************后端**************************************
        /// <summary>
        ///  获取成绩主表列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<ScoreStatistList> GetScoreStatistList(ScoreStatistParam model, out int total)
        {
            return DBSession.IT_VLinesExperienceDetailDAL.GetScoreStatistList(model, out total);
        }
        /// <summary>
        ///  获取成绩明细列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<ScoreStatistDetial> GetScoreStatistDetial(ScoreStatistDetail model, out int total)
        {
            return DBSession.IT_VLinesExperienceDetailDAL.GetScoreStatistDetial(model, out total);
        }
        /// <summary>
        ///  获取成绩主表导出数据table
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public DataTable GetScoreStatistExeclData(ScoreStatistParam model)
        {
            return DBSession.IT_VLinesExperienceDetailDAL.GetScoreStatistExeclData(model);
        }
        /// <summary>
        ///  获取成绩明细导出数据table
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public DataTable GetScoreStatistDetialExecl(ScoreStatistDetail model)
        {
            return DBSession.IT_VLinesExperienceDetailDAL.GetScoreStatistDetialExecl(model);
        }
        //*********************************接口****************************************
        /// <summary>
        ///  2.2获取体验成绩明细
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<ExperScoreResult> GetExperScoreMainList(ExperLineParam model)
        {
            List<ExperScoreResult> list = new List<ExperScoreResult>();
            //1.0 获取主表信息
            List<ExperScoreMain> mainList = DBSession.IT_VLinesExperienceDetailDAL.GetExperScoreMainList(model);
            if (mainList == null || mainList.Count <= 0)
                return null;
            //1.1 获取详细信息
            List<ExperScoreDetial> detailList = DBSession.IT_VLinesExperienceDetailDAL.GetExperScoreDetialList(model);
            mainList.ForEach(s =>
            {
                ExperScoreResult tempMain = new ExperScoreResult
                {
                    user_id = s.user_id,
                    user_name = s.user_name,
                    file_name = s.file_name,
                    file_code = s.file_code,
                    em_time = s.em_time,
                    score = s.score,
                    okNum = s.okNum,
                    errorNum = s.errorNum,
                    in_date = s.in_date,
                    out_date = s.out_date,
                    file_note = s.file_note,
                    folder_name = s.folder_name
                };
                if (s.errorNum <= 0)
                    tempMain.accuracy = 100;
                else if (s.okNum > 0)
                {
                    decimal temp = (Helper.ToDecimal(s.okNum) / Helper.ToDecimal(s.okNum + s.errorNum)) * 100;
                    tempMain.accuracy = Math.Round(temp, 2);
                }
                if(detailList!=null && detailList.Count > 0)
                {
                    var query = detailList.Where(o => o.user_id == s.user_id && o.file_id==s.file_id).ToList();
                    List<ExperScoreDetial> dList = new List<ExperScoreDetial>();
                    query.ForEach(n => dList.Add(n));
                    tempMain.detial = dList;
                }
                //缩略图列表
                List<FilePicList> picture = new List<FilePicList>();
                var bagPic = DBSession.IT_DocBagFileRecordDAL.Select(o => o.file_id == s.file_id);
                if (bagPic != null)
                {
                    bagPic.ForEach(n =>
                    {
                        picture.Add(new FilePicList
                        {
                            bag_id = n.bag_id,
                            file_type = n.file_type,
                            file_url = n.file_url.Replace("~", System.Web.HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, System.Web.HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")))
                        });
                    });
                }
                tempMain.picture = picture;
                list.Add(tempMain);            
            });
            return list;
        }
        /// <summary>
        ///  获取成绩统计
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<ExperScoreStatist> GetExperScoreStatist(ExperLineParam model)
        {
            return DBSession.IT_VLinesExperienceDetailDAL.GetExperScoreStatist(model);
        }
        /// <summary>
        ///  获取作品是否有人学习
        /// </summary>
        /// <param name="file_id"></param>
        /// <returns></returns>
        public bool GetLearnNumByFileId(int file_id, int user_id)
        {
            int num = DBSession.IT_VLinesExperienceDetailDAL.GetLearnNumByFileId(file_id,user_id);
            return num > 0 ? true : false;
        }
        /// <summary>
        ///  导航树path统计学习人数
        /// </summary>
        /// <param name="folderPath"></param>
        /// <returns></returns>
        public int CountLearnNumByPath(string folderPath)
        {
            return DBSession.IT_VLinesExperienceDetailDAL.CountLearnNumByPath(folderPath);
        }
    }
}